/*
 * @Author: zhouwr 1551541860@qq.com
 * @Date: 2023-12-07 15:40:32
 * @LastEditors: zhouwr 1551541860@qq.com
 * @LastEditTime: 2024-03-01 22:30:04
 * @FilePath: /jeecg-boot-master/jeecg-module-activiti/jeecg-module-activiti-biz/src/main/java/org/jeecg/modules/activiti/ModelSaveRestResource.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package org.jeecg.modules.activiti;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

@RestController
public class ModelSaveRestResource implements ModelDataJsonConstants {
  protected static final Logger LOGGER = LoggerFactory.getLogger(ModelSaveRestResource.class);

  @Autowired
  private RepositoryService repositoryService;

  @Autowired
  private ObjectMapper objectMapper;

  @RequestMapping(value = { "/model/{modelId}/save" }, method = {
      org.springframework.web.bind.annotation.RequestMethod.PUT })
  @ResponseStatus(HttpStatus.OK)
  public void saveModel(@PathVariable String modelId, @RequestParam MultiValueMap<String, String> values) {
    try {
      Model model = this.repositoryService.getModel(modelId);
      System.out.println("ModelSaveRestResource.saveModel----------");
      ObjectNode modelJson = (ObjectNode) this.objectMapper.readTree(model.getMetaInfo());
      modelJson.put("name", values.getFirst("name"));
      modelJson.put("description", values.getFirst("description"));
      model.setMetaInfo(modelJson.toString());
      model.setName(values.getFirst("name"));
      this.repositoryService.saveModel(model);
      this.repositoryService.addModelEditorSource(model.getId(), (values.getFirst("json_xml")).getBytes("utf-8"));

      // InputStream svgStream = new
      // ByteArrayInputStream((values.getFirst("svg_xml")).getBytes("utf-8"));
      // TranscoderInput input = new TranscoderInput(svgStream);
      // PNGTranscoder transcoder = new PNGTranscoder();
      // ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      // TranscoderOutput output = new TranscoderOutput(outStream);
      // transcoder.transcode(input, output);
      // byte[] result = outStream.toByteArray();
      // System.out.println(new String(result, "utf-8"));
      // this.repositoryService.addModelEditorSourceExtra(model.getId(), result);
      // outStream.close();
    } catch (Exception e) {
      LOGGER.error("Error saving model", e);
      throw new ActivitiException("Error saving model", e);
    }
  }
}
